<?php

// Formulaires

function printLoginForm($askedPage){
    echo <<<END
<div class="formLogin" id="formLogin">
    <form id="formLogin" action="index.php?action=login&page={$askedPage}" method="post">
    <table>
        <tr>
            <td class="label">Login : </td> <td class="input"><input name="login"/></td>
        </tr>
            <td class="label">Mot de passe : </td> <td class="input"><input type="password" name="password" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" id="submitFormLogin" value="Connexion" /></td>
        </tr>
    </table>
    </form>
</div>
END;
}
// Pour ceux qui ont javaScript...
function printCenteredLoginForm ($askedPage)
{
    echo <<<END
<div class="formLoginCentre" id="formLoginCentre">
    <div class="box">
    <div class="hautDroite">
        <a href="#" class="hideFormLogin"><img src="images/style/bouton_fermer.gif"/></a>
    </div>
    <form id="formLoginCentre" action="index.php?action=login&page={$askedPage}" method="post">
    <label>Login : </label> <input name="login"/><br/>
    <label>Mot de passe : </label><input type="password" name="password" /><br/>
    <input type="submit" id="submitFormLogin" value="Connexion" />
    </form>
    </div>
</div>
END;
}

function printLogoutForm($askedPage){
    $pseudo = pseudo() ;
    echo <<<END
<div class="formLogout">
    {$pseudo} <a href="index.php?action=logout&page={$askedPage}">déconnexion</a>


</div>
END;
}

// Connexion / déconnexion
function logIn(){
    $pass = md5($_POST['password']) ;
    $requete = "SELECT * FROM User WHERE pseudo = \"{$_POST['login']}\" AND password = \"{$pass}\";" ;
    connect() ;
    $result = executerRequete($requete, $GLOBALS['debug']) ;
    if(mysql_num_rows($result))
    {
        $row = mysql_fetch_assoc($result) ;
        $pseudo = $row['pseudo'] ;
        $niveau = $row['niveau'] ;
        $result = executerRequete("INSERT INTO Session (id, pseudo, date) VALUES (\"".session_id()."\", \"{$pseudo}\", NOW());", $GLOBALS['debug']) ;
        $GLOBALS['pseudo'] = $pseudo ;
        $GLOBALS['niveau'] = $niveau ;
    }else{
        alert("Erreur de login ou de mot de passe !") ;
    }
    mysql_close() ;
}

function logOut(){
    connect() ;
    executerRequete("DELETE FROM Session WHERE id = \"".session_id()."\";", $GLOBALS['debug']) ;
    mysql_close() ;
    unset($GLOBALS['pseudo']) ;
    unset($GLOBALS['niveau']) ;
    session_unset();
    session_destroy();
}

?>
